package com.teacold.customdemo.chapter_ten;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

@SuppressLint("AppCompatCustomView")
public class StrokeImage extends ImageView {
    public StrokeImage(Context context) {
        super(context);
    }

    public StrokeImage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StrokeImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Paint p=new Paint();
        p.setColor(Color.CYAN);
        setStateDrawable(this,p);

    }

    private void setStateDrawable(ImageView image, Paint p) {
        BitmapDrawable bd= (BitmapDrawable) image.getDrawable();
        Bitmap srcBmp=bd.getBitmap();
        Bitmap bitmap=Bitmap.createBitmap(srcBmp.getWidth(),srcBmp.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(bitmap);
        canvas.drawBitmap(srcBmp.extractAlpha(),0,0,p);
        //添加状态
        StateListDrawable stateListDrawable=new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed},new BitmapDrawable(bitmap));
        //setBackgroundDrawable()函数会移除原有的padding值。如果需要padding,则需要调用setPadding()函数
        image.setBackgroundDrawable(stateListDrawable);



    }


}
